package com.baylor.server.business.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baylor.server.base.util.WebUitl;
import com.baylor.server.business.entity.XiangAo;
import com.baylor.server.business.service.XiangAoService;

/**
 * 
 * @ClassName: XaHelloController
 * @Description: 测试 Controller
 * @author 曹文波
 * @date 2014年2月24日 上午10:30:23
 * 
 */

@Controller
@RequestMapping("/XaHello")
public class XaHelloController {
	
	@Autowired
	private XiangAoService xiangAoService;

	@RequestMapping(value = "hello")
	@ResponseBody
	public String hello() {
		String rep = "Hello 3ti!";
		return JSON.toJSONString(rep);
	}

	@RequestMapping(value = "helloByHtml")
	public String helloByHtml() {

		return "Hello";
	}
	
	@RequestMapping(value = "helloByJsp")
	public String helloByJsp() {

		return "Hello";
	}
	
	@RequestMapping(value = "findDataById", method = RequestMethod.POST)
	@ResponseBody
	public String findDataById(Long xiangAoId){
		return JSON.toJSONString(xiangAoService.findDataById(xiangAoId));
	}
	
	@RequestMapping(value = "createData", method = RequestMethod.POST)
	@ResponseBody
	public String createData(XiangAo xiangAo){
		return JSON.toJSONString(xiangAoService.createData(xiangAo));
	}
	
	
	@RequestMapping(value = "updateData", method = RequestMethod.POST)
	@ResponseBody
	public String updateData(XiangAo xiangAo){
		return JSON.toJSONString(xiangAoService.updateData(xiangAo));
	}
	
	@RequestMapping(value = "delData", method = RequestMethod.POST)
	@ResponseBody
	public String delData(XiangAo xiangAo){
		return JSON.toJSONString(xiangAoService.delData(xiangAo));
	}
	
	@RequestMapping(value = "findTestData", method = RequestMethod.POST)
	@ResponseBody
	public String findTestData(@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,String sort,@RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize){
		Pageable pageable = WebUitl.buildPageRequest(pageNumber,pageSize,sort);
		return JSON.toJSONString(xiangAoService.findTestData(pageable));
	}
	
	
	@RequestMapping(value = "findTestDataByFilter", method = RequestMethod.POST)
	@ResponseBody
	public String findTestDataByFilter(String jsonFilter,@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,String sort,@RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize){
		Pageable pageable = WebUitl.buildPageRequest(pageNumber,pageSize,sort);
		Map<String, Object> searchParams_ = WebUitl.getParametersStartingWith(jsonFilter, "search_");
		return JSON.toJSONString(xiangAoService.findTestDataByFilter(searchParams_, pageable));
	}
}
